<template>
    <div>
        <el-card class="!border-none" shadow="never">
            <el-form ref="formRef" class="mb-[-16px] mt-[16px]" :model="queryParams" :inline="true">
				<el-form-item label="商家名">
				    <el-input
				        class="w-[280px]"
				        v-model="queryParams.enterpris_name"
				        placeholder="商家名"
				        clearable
				        @keyup.enter="resetPage"
				    />
				</el-form-item>
				<el-form-item label="手机号">
				    <el-input
				        class="w-[280px]"
				        v-model="queryParams.mobile"
				        placeholder="手机号"
				        clearable
				        @keyup.enter="resetPage"
				    />
				</el-form-item>
                <el-form-item label="商户单号">
                    <el-input
                        class="w-[280px]"
                        v-model="queryParams.pay_sn"
                        placeholder="商户单号"
                        clearable
                        @keyup.enter="resetPage"
                    />
                </el-form-item>
                <el-form-item label="订单编号">
                    <el-input
                        class="w-[280px]"
                        v-model="queryParams.order_sn"
                        placeholder="订单编号"
                        clearable
                        @keyup.enter="resetPage"
                    />
                </el-form-item>
                <el-form-item label="下单时间">
                    <daterange-picker
                        v-model:startTime="queryParams.start_time"
                        v-model:endTime="queryParams.end_time"
                    />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                    <!-- <export-data
                        class="ml-2.5"
                        :fetch-fun="refundRecord"
                        :params="queryParams"
                        :page-size="pager.size"
                    /> -->
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <el-tabs v-model="activeTab" @tab-change="handleTabChange">
                <el-tab-pane
                    v-for="(item, index) in tabLists"
                    :label="`${item.name}(${pager.extend[item.numKey] ?? 0})`"
                    :name="index"
                    :key="index"
                >
                    <el-table size="large" border fit height="650" v-loading="pager.loading" :data="pager.lists">
						<el-table-column label="退款编号" prop="sn" min-width="190" align="center"/>
						<el-table-column label="订单编号" prop="order_sn" min-width="190" align="center"/>
						<el-table-column label="项目名称" prop="business_name" min-width="100" align="center"/>
						<el-table-column label="服务费用" prop="order_total_price" min-width="100" align="center"/>
						<el-table-column label="手机号" prop="mobile" min-width="120" align="center"/>
						<el-table-column label="实际支付" prop="order_amount" min-width="100" align="center"/>
						<el-table-column label="认证公司" prop="name" min-width="200" align="center"/>
						<el-table-column label="社会信用代码" prop="credit_code" min-width="220" align="center"/>
						<el-table-column label="法人代表姓名" prop="oper_name" min-width="100" align="center"/>
						<el-table-column label="法人证件号" prop="corporation_id_card" min-width="220" align="center"/>
						<el-table-column label="营业额" prop="turnover" min-width="150" align="center"/>
                        <el-table-column label="退款金额" min-width="100">
                            <template #default="{ row }"> ¥ {{ row.refund_amount }} </template>
                        </el-table-column>

                        <el-table-column label="退款类型" prop="refund_type_text" min-width="150" />
                        <el-table-column label="退款状态" prop="" min-width="100">
                            <template #default="{ row }">
								<el-tag type="info" v-if="row.refund_status == -1">
								    {{ row.refund_status_text }}
								</el-tag>
                                <el-tag type="warning" v-if="row.refund_status == 0">
                                    {{ row.refund_status_text }}
                                </el-tag>
                                <el-tag v-if="row.refund_status == 1">
                                    {{ row.refund_status_text }}
                                </el-tag>
                                <el-tag type="danger" v-if="row.refund_status == 2">
                                    {{ row.refund_status_text }}
                                </el-tag>
                            </template>
                        </el-table-column>
                        <el-table-column label="记录时间" prop="create_time" min-width="180" align="center"/>
                        <el-table-column label="操作" width="180" fixed="right" align="center">
                            <template #default="{ row }">
                                <el-button
                                    v-perms="['finance.refund/log']"
                                    type="primary"
                                    link
                                    @click="handleShowRefundLog(row.id)"
                                >
                                    退款日志
                                </el-button>
<!-- 								<el-button
								    v-if="row.refund_status == -1"
								    v-perms="['recharge.recharge/refundAgain']"
								    type="primary"
								    link
								    @click="handleRefund(row.id)"
								>
								    同意退款
								</el-button> -->
								<el-button v-if="row.refund_status == -1"
									v-perms="['recharge.recharge/refund']" type="primary" link
									@click="handleShowRefund(row)">
									退款操作
								</el-button>
                                <el-button
                                    v-if="row.refund_status == 2"
                                    v-perms="['recharge.recharge/refundAgain']"
                                    type="primary"
                                    link
                                    @click="handleRefund(row.id)"
                                >
                                    重新退款
                                </el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </el-tab-pane>
            </el-tabs>

            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <refund-log v-model="showRefundLog" :refund-id="selectRefundId" />
		<Refund v-if="showRefund" ref="editRef" @success="getLists" @close="showRefund = false" />
    </div>
</template>
<script lang="ts" setup name="articleLists">
import { refundRecord, refundAgain, refundStat } from '@/api/finance'
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import RefundLog from './component/refund-log.vue'
import Refund from './component/refund.vue'

const editRef = shallowRef<InstanceType<typeof Refund>>()
const showRefund = ref(false)
const queryParams = reactive({
    sn: '',
    order_sn: '',
    user_info: '',
    refund_type: '',
    start_time: '',
    end_time: '',
    refund_status: '',
	order_id:'',
	pay_sn:'',
	enterpris_name:'',
	mobile:''
})
const refundData = reactive({
    total: 0,
    ing: 0,
    success: 0,
    error: 0
})
const showRefundLog = ref(false)
const selectRefundId = ref(0)
const activeTab = ref(0)
const tabLists = ref([
    {
        name: '全部',
        type: '',
        numKey: 'total'
    },
	{
	    name: '申请退款',
	    type: -1,
	    numKey: 'shenqing'
	},
    {
        name: '待转账',
        type: 0,
        numKey: 'ing'
    },
    {
        name: '已退款',
        type: 1,
        numKey: 'success'
    },
    {
        name: '退款失败',
        type: 2,
        numKey: 'error'
    }
])

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: refundRecord,
    params: queryParams
})

const handleRefund = async (id: number) => {
    await feedback.confirm('确认重新退款？')
    await refundAgain({
        record_id: id
    })
    getLists()
    getRefundStat()
}

const handleShowRefundLog = async (id: number) => {
    showRefundLog.value = true
    selectRefundId.value = id
}
const handleTabChange = (index: any) => {
    queryParams.refund_status = tabLists.value[index].type as string
    resetPage()
}

	const handleShowRefund = async (row:any) => {
		console.log(row)
		showRefund.value = true
		await nextTick()
		editRef.value?.open('edit')
		console.log(row)
		row.refund_id = row.id
		row.id = row.order_id
		row.type = 1
		editRef.value?.setFormData(row)
	}

const getRefundStat = async () => {
    const res = await refundStat()
    Object.assign(refundData, res)
}
getRefundStat()
getLists()
</script>
